﻿using AutoMapper;
using Management.API.Application.Controller.Management.Material;
using Management.API.DTOs.Management;
using Management.Infrastructure.Interfaces.BOMManagement;
using MediatR;
using Management.Errorcode.ErrorCode;



namespace Management.API.Application.CommandHandler.Management.Material
{
    public class GetMaterialClassificationCommandHandler: IRequestHandler<GetMaterialClassificationCommand, ApiResult<IList<GetMaterialClassificationDTO>>>
    {
        private readonly IMaterialClassificationRepository iMaterialClassificationRepository;
        private readonly IMapper _mapper;
        private readonly ILogger<GetMaterialClassificationCommandHandler> _logger;

        public GetMaterialClassificationCommandHandler(IMaterialClassificationRepository IMaterialClassificationRepository, IMapper mapper,
            ILogger<GetMaterialClassificationCommandHandler> logger)
        {
            iMaterialClassificationRepository = IMaterialClassificationRepository;
            _mapper = mapper;
            _logger = logger;
        }

        public async Task<ApiResult<IList<GetMaterialClassificationDTO>>> Handle(GetMaterialClassificationCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var useList = await iMaterialClassificationRepository.GetAllAsync();
                var useLists = _mapper.Map<IList<GetMaterialClassificationDTO>>(useList);
                return ApiResult<IList<GetMaterialClassificationDTO>>.Success(useLists, StatusCodeEnum.Success);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "GetMaterialClassificationCommandHandler");
                throw;
            }

        }

    }
}
